<?php
namespace Home\Controller;
use Home\Controller\UserController;
use Home\Controller\BaseController;
class FavoritesController extends UserController
{
    public function _initialize(){
        parent::_initialize();
    }

    //显示 商品收藏/店铺关注
    public function index(){
//        $session = session();
        //商品收藏
        $favorites = M('Favorites');
        $goods = $favorites->where(array('member_id'=>session('user_id'),'fav_type'=>20))->select();
        if($goods){
            foreach($goods as $v){
                $goods_id[] = $v['fav_id'];
            }
//            $where['goods_sale'] = 1;
//            $where['goods_state'] = 1;
            $where['goods_id'] = array('in',$goods_id);
            $goods = M('goods')->where($where)->select();
            $this->assign('goods',$goods);
        }
        //店铺关注
        $store = $favorites->where(array('member_id'=>session('user_id'),'fav_type'=>40))->select();
        if($store){
            foreach($store as $v){
                $store_id[] = $v['fav_id'];
            }
            $store = M('store')->where(array('store_id'=>array('in',$store_id)))->select();
            $this->assign('store',$store);
        }
        $this->display();
    }

    //ajax添加 商品收藏/店铺关注
    public function favorites(){

        $datas['member_id'] = session('user_id');

        //fav_id 商品或店铺id
        $datas['fav_id'] = I('post.fav_id');
        //fav_type 商品类型为20   店铺类型为40
        $datas['fav_type'] = I('post.fav_type');
        $favorites = M('favorites');

        $re = $favorites->where($datas)->select();
        if($re){
            switch($datas['fav_type']){
                case 20 :
                    $data['n'] = '已收藏该商品';
                    $data['num'] = M('favorites')->where(array('fav_id'=>$datas['fav_id'],'fav_type'=>20))->count();
                    ;break;
                case 40 :
                    $data['num'] = $data['num'] = M('Favorites')->where(array('fav_id'=>$datas['fav_id'],'fav_type'=>40))->count();
                    $data['n'] = '该店铺已关注';
                    break;
            }
            $data['type'] = true;
            $this->ajaxReturn($data);
        }
        $datas['fav_time'] = time();

        $res = $favorites->data($datas)->add();
        if($res !== FALSE){
            switch($datas['fav_type']){
                case 20 :
                    $data['n'] = '商品收藏成功';
                    $data['num'] = M('favorites')->where(array('fav_id'=>$datas['fav_id'],'fav_type'=>20))->count();
                    break;
                case 40 :
                    M('store')->where(array('store_id'=>$datas['fav_id']))->setInc('store_collect',1);

                    $data['num'] = M('Favorites')->where(array('fav_id'=>$datas['fav_id'],'fav_type'=>40))->count();
                    $data['n'] = '店铺关注成功';
                    break;
            }
            $data['type'] = true;
            $this->ajaxReturn($data);
        }else{
            $data['type'] = false;
            $data['n'] = '出错，请稍后重试';
            $this->ajaxReturn($data);
        }
    }

    //删除收藏/取消关注
    public function del(){
        $where['member_id'] = session('user_id');
        $where['fav_id'] = I('post.data');
        $favorites = M('Favorites')->where($where)->delete();
        if($favorites){
            $this->success('操作成功', U('index'));
        }else{
            $this->error('网络链接失败!');
        }
    }

}